package com.cw.jeeyt.dao.impl.facade;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.cw.jeeyt.common.utils.StringUtil;
import com.cw.jeeyt.dao.intf.facade.FacadeEnvDao;
import com.cw.jeeyt.dao.mapper.facade.FacadeEnvMapper;
import com.cw.jeeyt.dao.model.facade.FacadeEnvDo;
import com.cw.jeeyt.dao.model.facade.FacadeEnvExample;
import com.cw.jeeyt.dao.model.facade.FacadeEnvKeyDo;
import com.cw.jeeyt.dao.query.facade.FacadeEnvQuery;
import com.cw.lang.common.dao.AbstractBaseDaoImpl;
import com.cw.lang.common.pagehelper.PageHelper;
import com.cw.lang.mybatis.dto.PageInfo;
import com.cw.lang.mybatis.dto.PageParam;

/**
 * 环境信息数据库操作接口实现
 *
 * @author chenw
 * @date 2019-10-31 22:35:52
 */
@Repository
public class FacadeEnvDaoImpl extends AbstractBaseDaoImpl<FacadeEnvDo, FacadeEnvMapper> implements FacadeEnvDao {
    /**
     * 分页查询
     *
     * @param facadeEnv 条件
     * @param pageParam 分页参数
     * @return
     */
    @Override
    public PageInfo<FacadeEnvDo> selectPage(FacadeEnvQuery facadeEnv, PageParam pageParam) {
        FacadeEnvExample example = buildExample(facadeEnv);
        return PageHelper.<FacadeEnvDo>startPage(pageParam.getPageNum(), pageParam.getPageSize()).doSelectPageInfo(
            () -> getMapper().selectByExample(example));
    }

    /**
     * 根据key查询环境信息
     *
     * @param id
     * @return
     */
    @Override
    public FacadeEnvDo selectByKey(Long id) {
        FacadeEnvKeyDo facadeEnvKeyDo = new FacadeEnvKeyDo();
        facadeEnvKeyDo.setId(id);
        return getMapper().selectByPrimaryKey(facadeEnvKeyDo);
    }

    /**
     * 根据key删除环境信息
     *
     * @param id
     * @return
     */
    @Override
    public int deleteByKey(Long id) {
        FacadeEnvKeyDo facadeEnvKeyDo = new FacadeEnvKeyDo();
        facadeEnvKeyDo.setId(id);
        return getMapper().deleteByPrimaryKey(facadeEnvKeyDo);
    }

    /**
     * 查询环境信息
     *
     * @param facadeEnv 条件
     * @return List<FacadeEnvDo>
     */
    @Override
    public List<FacadeEnvDo> selectByExample(FacadeEnvQuery facadeEnv) {
        return getMapper().selectByExample(buildExample(facadeEnv));
    }

    /**
     * 新增环境信息
     *
     * @param facadeEnv 条件
     * @return int>
     */
    @Override
    public int insertBySelective(FacadeEnvDo facadeEnv) {
        if (facadeEnv == null) {
            return -1;
        }

        return getMapper().insertSelective(facadeEnv);
    }

    /**
     * 修改环境信息
     *
     * @param facadeEnv
     * @return
     */
    @Override
    public int updateBySelective(FacadeEnvDo facadeEnv) {
        if (facadeEnv == null) {
            return -1;
        }
        return getMapper().updateByPrimaryKeySelective(facadeEnv);
    }

    /**
     * 构建环境信息Example信息
     *
     * @param facadeEnv
     * @return
     */
    public FacadeEnvExample buildExample(FacadeEnvQuery facadeEnv) {
        FacadeEnvExample example = new FacadeEnvExample();
        FacadeEnvExample.Criteria criteria = example.createCriteria();
        if (facadeEnv != null) {
            //添加查询条件
            if (null != facadeEnv.getId()) {
                criteria.andIdEqualTo(facadeEnv.getId());
            }
            if (StringUtil.isNotEmpty(facadeEnv.getEnvCode())) {
                criteria.andEnvCodeEqualTo(facadeEnv.getEnvCode());
            }
            if (StringUtil.isNotEmpty(facadeEnv.getEnvDesc())) {
                criteria.andEnvDescEqualTo(facadeEnv.getEnvDesc());
            }
            if (StringUtil.isNotEmpty(facadeEnv.getIpAddress())) {
                criteria.andIpAddressEqualTo(facadeEnv.getIpAddress());
            }
            if (null != facadeEnv.getPort()) {
                criteria.andPortEqualTo(facadeEnv.getPort());
            }
            if (null != facadeEnv.getCreateBy()) {
                criteria.andCreateByEqualTo(facadeEnv.getCreateBy());
            }
            if (null != facadeEnv.getCreateDateBegin()) {
                criteria.andCreateDateGreaterThanOrEqualTo(facadeEnv.getCreateDateBegin());
            }
            if (null != facadeEnv.getCreateDateEnd()) {
                criteria.andCreateDateLessThanOrEqualTo(facadeEnv.getCreateDateEnd());
            }
            if (null != facadeEnv.getUpdateBy()) {
                criteria.andUpdateByEqualTo(facadeEnv.getUpdateBy());
            }
            if (null != facadeEnv.getUpdateDateBegin()) {
                criteria.andUpdateDateGreaterThanOrEqualTo(facadeEnv.getUpdateDateBegin());
            }
            if (null != facadeEnv.getUpdateDateEnd()) {
                criteria.andUpdateDateLessThanOrEqualTo(facadeEnv.getUpdateDateEnd());
            }
        }
        return example;
    }
}
